package com.Travit;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.method.DateTimeKeyListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

public class ReportIllegalProd extends Activity{

	EditText barcode;
	EditText reportedby;
	Button turnin;
	Date reportingdate;
	//Date dateselected;
	SimpleDateFormat dateselected;
	String makedate;
	String invalidbarcode;
	String myass;
	String loggedid;
	private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;
	private static String urlReportInvalid = "http://project492.engr.sjsu.edu/travITinvalidBarcode.php";
	
	public void onCreate(Bundle savedInstanceState){
		Log.v("in", "retails");
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		setContentView(R.layout.reportillegal);
		loggedid = extras.getString("venid");
		invalidbarcode = extras.getString("invalidbarcode");
		barcode = (EditText) findViewById(R.id.reportedbarcode);
		invalidbarcode = "1";
		barcode.setText(invalidbarcode);
		//reportedby = findViewById(R.id.reporterinput).toString();
		//EditText address=(EditText)findViewById(R.id.reporterinput);
		myass = "me";
		//Log.v("reportedby", reportedby);
		
		turnin = (Button) findViewById(R.id.submitbutton);
		
		// capture our View elements
        mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
        mPickDate = (Button) findViewById(R.id.pickDate);

        // add a click listener to the button
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        
     // get the current date
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        // display the current date (this method is below)
        updateDisplay();
		
        
		turnin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//SHOW INFORMATION FROM BARCODE
				makedate = mDateDisplay.getText().toString().trim();
				Log.v("date is", makedate);
				Log.v("after submit", invalidbarcode);
				String validinvalid = "n";	
				myass = "imchanging";
				myass = myass.toLowerCase();
				HttpClient client = new DefaultHttpClient();
				
				//CHECK THE TYPE FOR REPORTER
				try {
					HttpPost request = new HttpPost(urlReportInvalid
							+ "?barcodeID=" + invalidbarcode
							+ "&reportingdate=" + makedate
							+ "&reportedby=" + "imme");
					Log.v("trying", "before execution");
					List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				    nameValuePairs.add(new BasicNameValuePair("barcodeID", "1"));
				    nameValuePairs.add(new BasicNameValuePair("reportingdate", makedate));
				    nameValuePairs.add(new BasicNameValuePair("reportedby", "imme"));
				    request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
		
					HttpResponse response = client.execute(request);
					HttpEntity entity = response.getEntity();
					String responseResult = EntityUtils.toString(entity);
					Log.v("executing", "" + responseResult);
					if(responseResult.equalsIgnoreCase("Updated Barcode Validity"))
					{
						Log.v("in if", "valid product");
						Intent newIntent = new Intent(v.getContext(), Retail.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
					else
					{
						Log.v("in else", "Updated Barcode Validity");
						Intent newIntent = new Intent(v.getContext(), Retail.class);
						newIntent.putExtra("venid", loggedid);
						startActivity(newIntent);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}
				Log.v("going back", "from outside ifelse");
				Intent newIntent = new Intent(v.getContext(), Retail.class);
				newIntent.putExtra("venid", loggedid);
				startActivity(newIntent);

			}			
		});

	}
	// updates the date in the TextView
    private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).append("-")
                    .append(mYear).append(" "));
    }
    // the callback received when the user "sets" the date in the dialog
    private DatePickerDialog.OnDateSetListener mDateSetListener =
            new DatePickerDialog.OnDateSetListener() {
    			@Override
                public void onDateSet(DatePicker view, int year, 
                                      int monthOfYear, int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateDisplay();
                }
            };

            @Override
            protected Dialog onCreateDialog(int id) {
                switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this,
                                mDateSetListener,
                                mYear, mMonth, mDay);
                }
                return null;
            }
}
